/*
 * PresenceData.java
 *
 * Created on July 5, 2004, 3:58 PM Modified for fun on 5/5/2005
 */

package midas.mw.crt.presence;
import java.io.Serializable;
import java.util.*;

import midas.mw.cfg.CfgError;
import midas.mw.cfg.CfgLogging;
import midas.mw.cfg.log.Logger;

/**
 * This class is used to consolidate data for presence. Add information here
 * for use in identifying a node or its status.
 */
public class PresenceKey implements Serializable
{    
    private static final long serialVersionUID = 1L;
    static final String seperator = ",";
    
    /**
     * JXTA node ID
     */
    private String nodeID;
    
    /**
     * MIDAS node name
     */
    private String nodeName;
    
    /**
     * MIDAS node type
     */
    private int nodeType;
    
    /**
     * MIDAS node name of the proxy which is connected to, if any
     */
    private String proxyName;

    /**
     * Long that represents last time a related profile was updated 
     */
    private long lastChanged;
    
    /**
     * Creates a new instance of PresenceKey
     * @param nodeID JXTA node ID
     * @param nodeName MIDAS node name
     * @param proxyName MIDAS node name of the proxy
     * @param lastChanged Last update of presence data. 0 for unknowns
     */
    public PresenceKey(String nodeID, String nodeName, int nodeType, String proxyName, long lastChanged) 
    {
    	CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PresenceKey(" + nodeID + ", " + nodeName + ", " + nodeType + ", " + 
    					proxyName + ", " + lastChanged + ")");
    	        
        this.nodeID = nodeID.replaceAll(",", "_").intern();
        this.lastChanged = lastChanged;
        this.nodeName = nodeName.replaceAll(",", "_").intern();        
        this.nodeType = nodeType;
        this.proxyName = proxyName;
    }
    
    /**
     * Creates a new instance of PresenceKey
     * @param nodeID JXTA node ID
     */
    public PresenceKey(String nodeID) 
    {
    	CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PresenceKey(" + nodeID + ")");
    	
        this.nodeID = nodeID.intern();        
    }
    
    /**
     * Create presence data from the formatted string.
     * @param fromString String with the presence data
     * @return PresenceKey instance
     */
    public static PresenceKey create(String fromString) 
    {
    	CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PresenceKey.create(" + fromString + ")");
    	
    	String[] result = fromString.split(seperator); 
    	if (result.length != 5){
    		throw new RuntimeException("result.length="+result.length+" cannot tokenize fromString="+fromString);
    	}
        
    	String nodeID = result[0];
    	String nodeName =result[1];
    	String nodeType =result[2];
    	String proxyName =result[3];
    	long lastChanged = 0L;
    	try {
    		if (result[4] != "")
    		   lastChanged = Long.parseLong(result[4]);           
    	} catch(NumberFormatException nfe){
    		CfgError.errorReporting(CfgError.ERR_OPER, Logger.COMPONENT_CRT, CfgError.SEV_WARNING, "PresenceKey.create(" + fromString + "). " + 					           
    				"Failed to parse long='" + result[4] + "'. Set last changed to now", null);    		
    		Date now = new Date();
    		lastChanged = now.getTime();
    	}
    	return new PresenceKey(nodeID, nodeName, Integer.parseInt(nodeType), proxyName, lastChanged);
    }
    
    /**
     * Creates a new instance of PresenceKey
     */
    public PresenceKey() 
    {
    	CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PresenceKey()");
    }
    
   /**
    * Returns a string with the content of presence data
    */
    public String toString()
    {
    	CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PresenceKey.toString()");
    	
        return nodeID+seperator
                +nodeName+seperator
                +nodeType+seperator
                +proxyName+seperator
                +lastChanged;
    }
    
    /**
     * Gets the JXTA node ID
     * @return JXTA node ID
     */
    public String getPresenceNodeID() 
    {
    	CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PresenceKey.getPresenceNodeID()");
    	
        return nodeID;
    }
    
    /**
     * Sets the JXTA node ID
     * @param nodeID JXTA node ID
     */
    public void setPresenceNodeID(String nodeID) 
    {
    	CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PresenceKey.setPresenceNodeID(" + nodeID + ")");
    	
        this.nodeID = nodeID.replaceAll(",", "_").intern();
    }

    /**
     * Gets the MIDAS node type
     * @return MIDAS node type
     */
    public int getNodeType() 
    {
    	CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PresenceKey.getNodeType()");
    	
        return nodeType;
    }
    
    /**
     * Sets the MIDAS node type
     * @param nodeType MIDAS node type
     */
    public void setNodeType(int nodeType) 
    {
    	CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PresenceKey.setNodeType(" + nodeType + ")");
    	
        this.nodeType = nodeType;
    }
    
    /**
     * Gets the last update of presence data
     * @return Long that represents last time a related profile was updated
     */
    public long getLastChanged() 
    {
    	CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PresenceKey.getLastChanged()");
    	
        return lastChanged;
    }
    
    /**
     * Sets the last update of presence data
     * @param lastChanged Long that represents last time a related profile was 
     * 			updated
     */ 
    public void setLastChanged(long lastChanged) 
    {
    	CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PresenceKey.setLastChanged(" + lastChanged + ")");
    	
        this.lastChanged = lastChanged;
    }
    
    /**
     * Gets the MIDAS node name
     * @return MIDAS node name
     */
    public String getPresenceName() 
    {
    	CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PresenceKey.getPresenceName()");
    	
        return nodeName;
    }
    
    /**
     * Sets the MIDAS node name
     * @param name MIDAS node name
     */
    public void setPresenceName(String name) 
    {
    	CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PresenceKey.setPresenceName(" + name + ")");
    	
        this.nodeName = name.replaceAll(",", "_").intern();
    }
    
    /**
     * Gets the MIDAS node name of the proxy
     * @return MIDAS node name of the proxy
     */
    public String getProxyName() 
    {
    	CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PresenceKey.getProxyName()");
    	
        return proxyName;
    }
    
    /**
     * Sets the MIDAS node name of the proxy
     * @param proxyName MIDAS node name of the proxy
     */
    public void setProxyName(String proxyName) 
    {
    	CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PresenceKey.setProxyName(" + proxyName + ")");
    	
        this.proxyName = proxyName.replaceAll(",", "_").intern();
    }
}